package com.xiaocq.wordskiller;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class UnitTest extends Activity {
	private static final String TAG = "UnitTest";

	private Button backToMenu;
	private ListView optionalUntiList;
	
	private int unitId;
	private int mFinishedCounts;
	private boolean testModel;
	private int continuePosition;
	private long allTimeInMillis;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.unittest);

		initComponents();

		loadOrInit();
	}

	private void loadOrInit() {
		SharedPreferences pref = getSharedPreferences(CommandList.PREF_NAME, MODE_PRIVATE);

		boolean isNewTest = pref.getBoolean(CommandList.IS_THE_NEW_TEST, true);
		allTimeInMillis = pref.getLong(CommandList.UNIT_TIME_COST, 0L);
		testModel = pref.getBoolean(CommandList.UNIT_TEST_MODEL, false);
		unitId = pref.getInt(CommandList.UNIT_ID, -1);
		if (!isNewTest && (unitId != -1)) {
			mFinishedCounts = pref.getInt(CommandList.UNIT_FINISHED_COUNTS, 0);
			new AlertDialog.Builder(UnitTest.this)
					.setMessage(R.string.tip_loadOrNot)
					.setPositiveButton(android.R.string.ok,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									sendIntent(unitId, mFinishedCounts, false, allTimeInMillis);
								}
							})
					.setNegativeButton(android.R.string.cancel,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
								}
							}).show();
		}
	}

	private void initComponents() {
		backToMenu = (Button) findViewById(R.id.unittest_backToMenu);
		optionalUntiList = (ListView) findViewById(R.id.unittest_optionalUnitList);
		ArrayAdapter<String> adapter = getData();
		optionalUntiList.setAdapter(adapter);

		backToMenu.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});

		optionalUntiList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				continuePosition = position + 1;
				new AlertDialog.Builder(UnitTest.this)
				.setItems(R.array.completion_or_multiple_choice, new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						testModel = which == 0 ? false : true;
						sendIntent(continuePosition, 0, true, 0);
					}
				}).show();
			}
		});
	}

	private ArrayAdapter<String> getData() {
		List<String> list = new ArrayList<String>();
		for (int i = 1; i <= 50; i++) {
			list.add("List " + i);
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(UnitTest.this,
				android.R.layout.simple_list_item_1, list);
		return adapter;
	}

	private void sendIntent(int position, int finishedCounts, boolean newTest, long allTime) {
		Intent intent = new Intent(testModel ? CommandList.UNIT_COMPLETION_TEST : CommandList.UNIT_CHOICE_TEST);
		intent.putExtra(CommandList.UNIT_ID, position);
		intent.putExtra(CommandList.UNIT_FINISHED_COUNTS, finishedCounts);
		intent.putExtra(CommandList.IS_THE_NEW_TEST, newTest);
		intent.putExtra(CommandList.UNIT_TIME_COST, allTime);
		startActivity(intent);
	}
}
